package com.atbeijing.write;

import com.atbeijing.bean.Teacher;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Index;
import java.io.IOException;

/**
 * 写入es一条
 * es 的template
 *
     #teacher_zhangsan
     PUT _template/template_teacher
     {
     "index_patterns": ["teacher_*"],
     "aliases" : {
     "{index}-query": {},
     "teacher-query":{}
     },
     "mappings": {
     "_doc": {
     "properties": {
     "id": {
     "type": "long"
     },
     "name": {
     "type": "text"
     },
     "age": {
     "type": "integer"
     },
     "sex": {
     "type": "keyword"
     },
     "hobby": {
     "type": "text"
     }
     }
     }
     }
     }
 */
public class WriteIndex {
    public static void main(String[] args) throws IOException {
        //1.获取jest客户端工厂
        JestClientFactory jestClientFactory = new JestClientFactory();

        //2.设置连接ES地址
        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop202:9200").build();
        jestClientFactory.setHttpClientConfig(httpClientConfig);

        //3.获取连接
        JestClient jestClient = jestClientFactory.getObject();

        //4.操作数据，往ES中写入数据
        //6.0~7.0一个索引有一个文档类型,多个文档id,一个文档id就是一条记录
        Teacher t1 = new Teacher(2L,"lisi",19,"男","洗头,泡澡,足浴,骑行");//文档内容
        Index index = new Index.Builder(t1)
                .index("teacher_lisi")//索引
                .type("_doc")//文档类型
                .id("T2")//文档id
                .build();
        jestClient.execute(index);


        //最后一步：关闭连接
        jestClient.shutdownClient();
    }
}
